<?php

class SoftModule {

    public function login() {
        global $_FANWE;

        $username = htmlspecialchars(trim($_POST['username']));
        $password = htmlspecialchars(trim($_POST['password']));
        if(empty($username) || empty($password)) exit('1');
        $sql = "SELECT * FROM soft_users WHERE username='{$username}' AND password='{$password}'";
        $user = FDB::fetchFirst($sql);
        if(!$user) {
            exit('2');
        }

        if(!empty($user['pid'])) {
            $sql = "SELECT * FROM soft_users WHERE userid={$user['pid']}";
            $puser = FDB::fetchFirst($sql);
            $authkey = 'W' . $puser['authkey'];
        } else {
            $authkey = 'F' . $user['authkey'];
        }

        echo '0#'.$authkey;
    }

    public function unlock() {
        global $_FANWE;

        if(!strlen($_POST['mobile']) == 11 || !is_numeric($_POST['mobile'])) {
            exit('1#错误的手机号码');
        }

        $sql = "SELECT * FROM soft_sess WHERE mobile='{$_POST['mobile']}'";
        $sess = FDB::fetchFirst($sql);
        if(empty($sess)) {
            exit('1#手机验证码错误');
        }

        FDB::query("DELETE FROM soft_sess WHERE id={$sess['id']}");
        if($sess['code'] != $_POST['code']) {
            exit('1#验证码错误，请重新获取验证码');
        }

        $sql = "SELECT * FROM soft_users WHERE username='{$_POST['mobile']}'";
        $user = FDB::fetchFirst($sql);
        if(empty($user)) {
            exit('1#主管不存在');
        }

        exit('0');
    }
}